package cn.tedu._07springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;

@Aspect
@Component
public class LogAspect {
    @Pointcut("execution(public int cn.tedu._07springaop.aop.CalculatorIml.*(..))")
    public void doTime(){
        System.out.println("执行切点方法...");

    }
   @Before("doTime()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println("方法执行前...");
        String name = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("方法名："+name+"，参数："+args);
    }
    @After("doTime()")
    public void afterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("后置通知"+name+"方法结束了");
    }
    @AfterReturning(value = "doTime()",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        String name = joinPoint.getSignature().getName();
        System.out.println("方法名："+name+"，返回值："+result);
    }
    @AfterThrowing(value = "doTime()", throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Throwable e){
        String name = joinPoint.getSignature().getName();
        System.out.println("方法名："+name+"，异常："+e.getMessage());
    }

    @Around(value = "doTime()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        Object result =null;
        try {
            System.out.println("前置通知...");
            result =joinPoint.proceed();
            System.out.println("返回通知...");
        } catch (Throwable e) {
            System.out.println("异常通知");
        }finally {
            System.out.println("后置通知...");
        }
        return result;
    }
}
